package com.fruit.controller.app.shopping;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fruit.controller.base.BaseController;
import com.fruit.pojo.shopping.Shopping;
import com.fruit.service.shopping.ShoppingManager;
import com.fruit.util.ResultInfo;


@Controller
@RequestMapping(value = "/app/shopping")
public class ShoppingController extends BaseController {
	
	@Autowired
	private ShoppingManager shoppingService;

	/**
	 * 查看购物车列表
	 * @param userId
	 * @return
	 * @author admin
	 * @version 2017
	 */
	@RequestMapping
	@ResponseBody
	public Map<String, Object> getList(int userId){
		this.logBefore(logger, "购物车列表");
		Map<String, Object> resMap = new HashMap<String, Object>();
		try{
			resMap = shoppingService.createGetShoppingList(userId);
	
		}catch(Exception e){
			e.printStackTrace();
			this.logError(logger, e.getMessage());
			resMap.put("result", this.initResult(ResultInfo.ERROR, "expection", "系统繁忙!"));
		}
		
		
		return resMap;
	}
	
	
	
	/**
	 * 添加购物车
	 * @param userId
	 * @return
	 * @author admin
	 * @version 2017
	 */
	@RequestMapping(value="/addShopping",method = { RequestMethod.POST,RequestMethod.GET })
	@ResponseBody
	public Map<String, Object> addShopping(Shopping shopping){
		this.logBefore(logger, "添加购物车");
		Map<String, Object> resMap = new HashMap<String, Object>();
		try{
			resMap = shoppingService.addShopping(shopping);
	
		}catch(Exception e){
			e.printStackTrace();
			this.logError(logger, e.getMessage());
			resMap.put("result", this.initResult(ResultInfo.ERROR, "expection", "系统繁忙!"));
		}
		
		
		return resMap;
	}	
	
	
	/**
	 * 删除购物车
	 * @param userId
	 * @return
	 * @author admin
	 * @version 2017
	 */
	@RequestMapping(value="/delShopping",method = { RequestMethod.POST,RequestMethod.GET })
	@ResponseBody
	public Map<String, Object> delShopping(Shopping shopping){
		this.logBefore(logger, "删除购物车");
		Map<String, Object> resMap = new HashMap<String, Object>();
		try{
			resMap = shoppingService.delShopping(shopping);
	
		}catch(Exception e){
			e.printStackTrace();
			this.logError(logger, e.getMessage());
			resMap.put("result", this.initResult(ResultInfo.ERROR, "expection", "系统繁忙!"));
		}
		
		
		return resMap;
	}		
	
	
	/**
	 * 修改购物车
	 * @param userId
	 * @return
	 * @author admin
	 * @version 2017
	 */
	@RequestMapping(value="/updateShopping",method = { RequestMethod.POST,RequestMethod.GET })
	@ResponseBody
	public Map<String, Object> updateShopping(Shopping shopping){
		this.logBefore(logger, "添加购物车");
		Map<String, Object> resMap = new HashMap<String, Object>();
		try{
			resMap = shoppingService.updateShopping(shopping);
	
		}catch(Exception e){
			e.printStackTrace();
			this.logError(logger, e.getMessage());
			resMap.put("result", this.initResult(ResultInfo.ERROR, "expection", "系统繁忙!"));
		}
		
		
		return resMap;
	}
	
	/**
	 * 获取购物车商品数量
	 * @param userId
	 * @return 
	 * @author admin
	 * @version 2017
	 */
	@RequestMapping(value="/getShoppingNum",method = { RequestMethod.POST,RequestMethod.GET })
	@ResponseBody
	public Map<String, Object> getShoppingNum(Shopping shopping){
		this.logBefore(logger, "购物车商品数量");
		Map<String, Object> resMap = new HashMap<String, Object>();
		try{
			resMap = shoppingService.getShoppingNum(shopping);
	
		}catch(Exception e){
			e.printStackTrace();
			this.logError(logger, e.getMessage());
			resMap.put("result", this.initResult(ResultInfo.ERROR, "expection", "系统繁忙!"));
		}
		
		
		return resMap;
	}
	

}
